package com.uwaterloo.interviewme;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.uwaterloo.interviewme.am.ApplicationManager;
import com.uwaterloo.interviewme.common.DBConstants;
import com.uwaterloo.interviewme.exception.ServerException;
import com.uwaterloo.interviewme.model.Interview;

/**
 * The class {@link InterviewListRender} will display brief information of all the interviews of the
 * user as a list.
 * 
 * Users can only sees the essential information in this view. If they wants more information for a
 * particular interview, they can click on any of the interviews in the list and
 * {@link InterviewListRender} will call the {@link DetailInterviewRender} view to render the detail
 * information
 */
public class InterviewListRender extends ListActivity {

	/** The Constant SETTING. */
	private static final int SETTING = Menu.FIRST;

	/** The Constant DELETE_INTERVIEW. */
	private static final int DELETE_INTERVIEW = Menu.FIRST + 1;

	/** The Constant CNAME. */
	private static final String CNAME = "companyName";

	/** The Constant POSITION. */
	private static final String POSITION = "position";

	/** The Constant LOCATION. */
	private static final String LOCATION = "location";

	/** The Constant TIME. */
	private static final String TIME = "time";

	/** The application manager. */
	private ApplicationManager am;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.interview_list);

		/* Init application manager */
		am = ApplicationManager.getInstance();
		am.setContext(this);

		renderInterviewList();
		registerForContextMenu(getListView());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, SETTING, 0, R.string.settings);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case SETTING:
			Intent intent = new Intent(this, Settings.class);
			startActivity(intent);
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View,
	 * android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_INTERVIEW, 0, R.string.delete_interview);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_INTERVIEW:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			am.deleteInterview((int) info.id);
			renderInterviewList();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View,
	 * int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(this, DetailInterviewRender.class);
		intent.putExtra(DBConstants.ROW_ID, (int) id);
		startActivity(intent);
	}

	/**
	 * Render interview list.
	 */
	private void renderInterviewList() {
		try {
			Collection<Interview> interviews = am.loadInterviewList();
			List<Map<String, String>> iList = new ArrayList<Map<String, String>>();
			for (Interview interview : interviews) {
				Map<String, String> iMap = new HashMap<String, String>();
				iMap.put(CNAME, interview.getCompanyName());
				iMap.put(POSITION, interview.getPosition());
				iMap.put(LOCATION, interview.getLocation());
				iMap.put(TIME, interview.getTime());
				iList.add(iMap);
			}

			String[] from = new String[] { CNAME, POSITION, LOCATION, TIME };
			int[] to = new int[] { R.id.companyName, R.id.position, R.id.location, R.id.time };
			SimpleAdapter adapter = new SimpleAdapter(this, iList, R.layout.interview_row, from, to);
			setListAdapter(adapter);
		} catch (ServerException e) {
			Toast.makeText(this, "Server unavailable. Try again later", Toast.LENGTH_LONG).show();
		}

	}
}
